<?php
/**
*
* http://framework.zend.com/wiki/pages/viewpage.action?pageId=10158667
*
*/


class Sgit_Validate_DateGreaterThan extends Zend_Validate_Abstract
{
 
    const NOT_GREATER = 'notGreaterThan';
 
    protected $_messageTemplates = array(
        self::NOT_GREATER => "'%value%' is not greater than '%min%'"
    );
 
    protected $_messageVariables = array(
        'min' => '_min'
    );
 
    protected $_min;
 
    protected $_inclusive;
     
    protected $_datePart;
 
    public function __construct($min, $datePart = Zend_Date::DATES )
    {
        
        
        
        if( $min instanceof Zend_Date ){
             $this->setMin( $min )->setDatePart( $datePart );
        }else{
            $min = Zend_Controller_Front::getInstance()->getRequest()->getParam($min);
            if(!empty($min) ){
                 $min=new Zend_Date($min,Zend_Date::ISO_8601);
                 $this->setMin( $min )->setDatePart( $datePart );
            }
        
        }

    }
 
    public function getMin()
    {
        return $this->_min;
    }
 
    public function setMin( Zend_Date $min )
    {
        $this->_min = $min;
        return $this;
    }
 
    public function getDatePart()
    {
        return $this->_datePart;
    }
 
    public function setDatePart( $datePart )
    {
        $this->_datePart = $datePart;
        return $this;
    }
 
    public function isValid( $value )
    {
        if( !$value instanceof Zend_Date )
        {
            try
            {
                $value = new Zend_Date( $value ,Zend_Date::ISO_8601);
            }
            catch( Exception $exception )
            {
                require_once 'Zend/Validate/Exception.php';
                throw new Zend_Validate_Exception( $exception->getMessage() );
            }
        }
 
        $this->_setValue( $value );

        $validator = new Zend_Validate_Date();
        
        if(!$validator->isValid($this->_min)){
            $this->_error( self::NOT_GREATER );
            return false;
        }
        


        if( $this->_min->isLater( $value, $this->_datePart ) )
        {
            $this->_error( self::NOT_GREATER );
            return false;
        }
 
        return true;
    }
 
}